package myflags

import (
	"flag"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	"github.com/sirupsen/logrus"
	"os"
	"study/server/global"
	"study/server/model/system"
)

type option struct {
	DB   bool
	File string
	Menu string
	Type string
}

var Opt option

func init() {
	flag.StringVar(&Opt.File, "f", "server/config/config.yaml", "配置文件路径")
	flag.StringVar(&Opt.Menu, "m", "", "菜单")
	flag.StringVar(&Opt.Type, "t", "", "类型")
	flag.BoolVar(&Opt.DB, "b", true, "数据库迁移")
	flag.Parse()
}

func InitFlag() {
	if Opt.DB {
		// 数据表迁移
		var err = global.DB.AutoMigrate(
			&system.UserModel{},
			&system.RoleModel{},
			&system.MenuModel{},
			&system.ApiModel{},
			&gormadapter.CasbinRule{},
		)
		if err != nil {
			logrus.Fatalf("数据库迁移失败 %s", err)
			os.Exit(0)
		}
		logrus.Info("数据库迁移成功")
	}
	switch Opt.Menu {
	case "user":
		switch Opt.Type {
		case "init":
			InitAdmin()
			os.Exit(0)
		}
	}
}
